package com.thssh.ui.lsn13.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/12
 * email: zhangyugehu@163.com
 */

public class CustomView extends View {

    private boolean mOnTouchEventHandled;
    private boolean mDisallowIntercept;

    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnTouchEventHandled(boolean handled){
        mOnTouchEventHandled = handled;
    }

    public void setDisallowIntercept(boolean intercept) {
        this.mDisallowIntercept = intercept;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
//        getParent().requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        getParent().requestDisallowInterceptTouchEvent(mDisallowIntercept);
        boolean handled = mOnTouchEventHandled;
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("CustomView", "onTouchEvent: ACTION_DOWN " + handled);
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("CustomView", "onTouchEvent: ACTION_MOVE " + handled);
                break;
            case MotionEvent.ACTION_UP:
                Log.d("CustomView", "onTouchEvent: ACTION_UP " + handled);
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d("CustomView", "onTouchEvent: ACTION_CANCEL " + handled);
                break;
            default:
                Log.d("CustomView", "onTouchEvent: " + event.getAction() + "  " + handled);
                break;
        }
        return handled;
    }
}
